package com.yuyou.tas.lesson.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuyou.tas.lesson.mapper.LessonClassChapterMapper;
import com.yuyou.tas.lesson.service.IBaseChapterService;
import com.yuyou.tas.lesson.service.ILessonChapterService;
import com.yuyou.tas.lesson.service.ILessonClassChapterService;
import com.yuyou.tas.lesson.service.comm.OfficeService;
import com.yuyou.tas.service.api.admin.dto.OfficeTokenDTO;
import com.yuyou.tas.service.api.admin.service.OfficeTokenAPI;
import com.yuyou.tas.service.api.lesson.entity.BaseChapter;
import com.yuyou.tas.service.api.lesson.entity.LessonChapter;
import com.yuyou.tas.service.api.lesson.entity.LessonClassChapter;
import com.yuyou.tas.util.bean.FileMeta;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.bean.ResourceObj;
import com.yuyou.tas.util.enums.CommEnums;
import com.yuyou.tas.util.tool.Sequence;

@Service
public class LessonClassChapterServiceImpl implements ILessonClassChapterService{

	
	@Autowired
	private LessonClassChapterMapper lessonClassChapterMapper;
	
	@Autowired
	private ILessonChapterService lessonChapterService;
	
	@Autowired
	private IBaseChapterService baseChapterService;
	
	@Autowired
	private OfficeService officeService;
	
	@Autowired
	private OfficeTokenAPI officeTokenAPI;

	
	@Override
	public int deleteByPrimaryKey(Long id) {
		return lessonClassChapterMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteByLessonClassId(Long lessonClassId) {
		return lessonClassChapterMapper.deleteByLessonClassId(lessonClassId);
	}


	@Override
	public int insert(LessonClassChapter record) {
		record.setId(Sequence.getSequence().nextId());
		record.setCreateTime(new Date());
		return lessonClassChapterMapper.insert(record);
	}

	@Override
	public LessonClassChapter selectByPrimaryKey(Long id) {
		return lessonClassChapterMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(LessonClassChapter record) {
		return lessonClassChapterMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<LessonClassChapter> findBylesChaId(Long lessonChapterId) {
		return lessonClassChapterMapper.findBylesChaId(lessonChapterId);
	}

	@Override
	public List<LessonClassChapter> findByLessonClassId(Long lessonClassId) {
		return lessonClassChapterMapper.findByLessonClassId(lessonClassId);
	}
    /**
     * 复制课程讲次到班级讲次（课件,讲义）
     * @param lessonClassChapterId
     * @param type "类型(1:课件，2：讲义
     * @return
     */
	@Override
	@Transactional(rollbackFor=Exception.class,timeout=120)
	public ResBoolSimpleInfo<OfficeTokenDTO> copyLessonChapter(Long lessonClassChapterId, Integer type,LoginRedisObj loginRedisObj) throws Exception {
		
		LessonClassChapter lcc=this.selectByPrimaryKey(lessonClassChapterId);
		LessonChapter lc =lessonChapterService.selectByPrimaryKey(lcc.getLessonChapterId());		
		BaseChapter b=baseChapterService.selectByPrimaryKey(lc.getBaseChapterId());		
		ResourceObj resourceObj=new ResourceObj(loginRedisObj.getUserId(),lcc.getLessonClassId(),loginRedisObj.getOrganizationId());
		
		LessonClassChapter update =new LessonClassChapter();
		update.setId(lcc.getId());
		
		String resourceId=StringUtils.EMPTY;
		
		switch (type) {
		case 1:
			
			//机构复制权限：
			ResBoolSimpleInfo<String> resCourPer=officeTokenAPI.hasResourcePer(loginRedisObj.getOrganizationId(),1);
			if(!resCourPer.isSuccess()){
				throw new Exception("没有权限");
			}
			
			//已经复制过，直接返回
			if(!StringUtils.isEmpty(lcc.getCourseUrl())){
				resourceId=lcc.getCourseUrl();
			}else{				
				FileMeta fileMetaCourse=officeService.copyFileMeta(CommEnums.resourceType.COURSE.getValue(),b.getCourseUrl(), resourceObj);			
				update.setCourseUrl(fileMetaCourse.getResourceId());
				update.setCourseName(fileMetaCourse.getName());
				update.setCourseSize(new BigDecimal(fileMetaCourse.getSize()));	
				this.updateByPrimaryKeySelective(update);
				resourceId=fileMetaCourse.getResourceId();
			}
			break;
		case 2:
			
			//机构复制权限：
			ResBoolSimpleInfo<String> resLecPer=officeTokenAPI.hasResourcePer(loginRedisObj.getOrganizationId(),2);
			if(!resLecPer.isSuccess()){
				throw new Exception("没有权限");
			}
			
			if(!StringUtils.isEmpty(lcc.getLectureUrl())){
				resourceId=lcc.getLectureUrl();
			}else{				
				FileMeta fileMetaLec=officeService.copyFileMeta(CommEnums.resourceType.LECTURE.getValue(),b.getLectureUrl(), resourceObj);			
				update.setLectureUrl(fileMetaLec.getResourceId());
				update.setLectureName(fileMetaLec.getName());
				update.setLectureSize(new BigDecimal(fileMetaLec.getSize()));	
				this.updateByPrimaryKeySelective(update);
				resourceId=fileMetaLec.getResourceId();
			}
			break;
		default:
			throw new Exception("类型不正确");
		}
		
		ResBoolSimpleInfo<OfficeTokenDTO> res=officeTokenAPI.editNoCheck(resourceId);
		return res;
	}



}